#region Header
//=============================================================================
//                          GShield (Game Shield)
//=============================================================================
//
// Original Programmer: Shawn Jackson
// Company: WaveTech Digital Technologies (www.wtdt.com)
// Copyright: (c) 2006 WaveTech Digital Technologies, All Rights Reserved.
// Assembly: Engine
// Code File Name: PathCleaner.cs
// Code File Date: 5/14/2007
//
// Code File Info: Utilities to clean directory paths
//=============================================================================
/*
 * UPDATES
 * -------
 */
#endregion Header

#region License
/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
#endregion License

#region Includes
using System;
using WaveTech.GShield.Data;
using System.Collections.Generic;
#endregion Includes

namespace WaveTech.GShield.Engine.Tools
{
	/// <summary>
	/// Utilities to clean directory paths
	/// </summary>
	public static class PathCleaner
	{
		#region Clean
		/// <summary>
		/// Takes in a path, minus a file name, and removes common sub directories, like \bin.
		/// </summary>
		/// <param name="path">Full path to pare</param>
		/// <returns>Path without common sub directories</returns>
		public static string Clean(string path)
		{
			if (path == null || path.Length <= 0)
				throw new ArgumentNullException("path");

			string CleanedPath = path;
			string[] DirectoryNames = path.Split(Char.Parse("\\"));

			// Remove directory names less then 4 charaters.
			foreach (string s in DirectoryNames)
			{
				if (s.Length < 4)
					if (CleanedPath.IndexOf("\\" + s) >= 0)
						CleanedPath = CleanedPath.Remove(CleanedPath.IndexOf("\\" + s), s.Length + 1);
			}

            List<string> exclusionNames = DirectoryNameExclusionsDAO.GetAllDirectoryNameExclusions();

			// Removes special directory names.
            foreach (string s in exclusionNames)
			{
				string NameToStrip = "\\" + s;

				while (CleanedPath.Contains(NameToStrip))
				{
					CleanedPath = CleanedPath.Remove(CleanedPath.IndexOf(NameToStrip), NameToStrip.Length);
				}
			}

			return CleanedPath;
		}
		#endregion Clean

		#region Strip File Name
		/// <summary>
		/// Takes in a full path and removes the file name.
		/// </summary>
		/// <param name="fullPath">Full path to pare</param>
		/// <returns>Path without file name</returns>
		public static string StripFileName(string fullPath)
		{
			if (fullPath == null || fullPath.Length <= 0)
				throw new ArgumentNullException("fullPath");

			return fullPath.Substring(0, fullPath.LastIndexOf(char.Parse("\\")));
		}
		#endregion Strip File Name
	}
}